// pages/registration/longvisitorhistory.js
const app = getApp()
var Constant = require('../../utils/constant.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userList: [],
    tempList:[],
    editIndex: 0,
    delBtnWidth: 150,//删除按钮宽度单位（rpx）
    itemClick: false,
    animationData: '',
    selectArray: [{ "id": "1", "text": "全部" },
                 { "id": "2", "text": "业主配偶" }, 
                 { "id": "3", "text": "业主父母" },
                 { "id": "4", "text": "业主兄弟" },
                 { "id": "5", "text": "业主子女" },
                 { "id": "6", "text": "业主亲戚" },
                 { "id": "1", "text": "其它" }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getLongList(this);
    scaleButton(this, 0, 0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    getLongList(this)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //选择回调
  getDate: function (e) {
    if(this.data.userList.length > 0){
        switch (e.detail.text){
          case '全部':
          this.setData({
          tempList:this.data.userList,
          })
          // console.log(tempList);
          break;
          case '业主配偶':
            this.data.tempList = [];
            for(var i = 0;i<this.data.userList.length;i++){
              if (this.data.userList[i].personrelation == '关系：业主配偶'){
                this.data.tempList.push(this.data.userList[i]);
              }
            }
            this.setData({
              tempList: this.data.tempList,
            })
          break;
          case '业主父母':
            this.data.tempList = [];
            for (var i = 0; i < this.data.userList.length; i++) {
              if (this.data.userList[i].personrelation == '关系：业主父母') {
                this.data.tempList.push(this.data.userList[i]);
              }
            }
            this.setData({
              tempList: this.data.tempList,
            })
            break;
          case '业主兄弟':
            this.data.tempList = [];
            for (var i = 0; i < this.data.userList.length; i++) {
              if (this.data.userList[i].personrelation == '关系：业主兄弟') {
                this.data.tempList.push(this.data.userList[i]);
              }
            }
            this.setData({
              tempList: this.data.tempList,
            })
            break;
          case '业主子女':
            this.data.tempList = [];
            for (var i = 0; i < this.data.userList.length; i++) {
              if (this.data.userList[i].personrelation == '关系：业主子女') {
                this.data.tempList.push(this.data.userList[i]);
              }
            }
            this.setData({
              tempList: this.data.tempList,
            })
            break;
          case '业主亲戚':
            this.data.tempList = [];
            for (var i = 0; i < this.data.userList.length; i++) {
              if (this.data.userList[i].personrelation == '关系：业主亲戚') {
                this.data.tempList.push(this.data.userList[i]);
              }
            }
            this.setData({
              tempList: this.data.tempList,
            })
            break; 
          case '其它':
            this.data.tempList = [];
            for (var i = 0; i < this.data.userList.length; i++) {
              if (this.data.userList[i].personrelation == '关系：其它') {
                this.data.tempList.push(this.data.userList[i]);
              }
            }
            this.setData({
              tempList: this.data.tempList,
            })
            break; 
        }
    }
  },

  deleteUser: function(e){
    var index = e.currentTarget.dataset.index;
    var that = this;
    var deleteStr = deleteIdList.join();
    // for (var i = 0; i < that.data.userList.length; i++) {
    //   if (that.data.userList[i].choose) {
    //     deleteStr = 
    //   }
    // }
    console.log(deleteStr)
    wx.showModal({
      title: '警告',
      content: '确定要删除该亲属吗?',
      success: function(res){
        if(res.confirm){
          wx.showLoading({
            title: '删除中',
          })
          wx.request({
            url: Constant.BASEURL + Constant.DELETE_LONG_USER + '&allid=' + deleteStr + "&openid=" + app.globalData.openid,
            method: 'GET',
            success: function (res) {
              console.log(res.data)
              getLongList(that)
              that.setData({
                itemClick: false,
              })
              scaleButton(that, 0, 0);
            },
            complete: function () {
              wx.hideLoading();
            }
          });

        }else{
          console.log("制止了删除")
        }
      }
    })
  },
  shareClick:function(e){
    
    var index = e.currentTarget.dataset.index;
    var that = this;
    console.log("aaaa", that.data.tempList[index]);
    var deleteStr = that.data.tempList[index].id;
    
    
    wx.showModal({
      title: '提示',
      content: '确定要为该亲属添加分享吗?',
      success: function(res){
        if(res.confirm){
          wx.request({
            url: Constant.BASEURL + Constant.CHANGE_SHARE_TYPE + '&id=' + deleteStr,
            method:'GET',
            success: function (res) {
// <<<<<<< Updated upstream
              
              if (res.data.respCode == '0'){
                getLongList(that)
                that.setData({
                  itemClick: false,
                })
                scaleButton(that, 0, 0);
                wx.showToast({
                  title: '授权成功',
                  icon: 'none'
                })
              }else{
                wx.showToast({
                  title: '授权失败',
                  icon: 'none'
                })
              }
              
// =======
//               //console.log(res.data)
//               getLongList(that)
//               that.setData({
//                 itemClick: false,
//               })
//               scaleButton(that, 0, 0);
//               wx.showToast({
//                 title: '授权成功',
//                 icon: 'none'
//               })
// >>>>>>> Stashed changes
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        }else{
        console.log("制止了改变")
      }
      }
      });
      },
  onItemClick: function(e){
    var that = this;
    let show_button = false;
    deleteIdList = [];
    var index = e.currentTarget.dataset.index;
    var checked = !that.data.tempList[index].choose;
    var update = 'tempList[' + index + '].choose';
    that.setData({
      [update]: checked,
    })
    for (var i = 0; i < that.data.tempList.length;i++){
      if (that.data.tempList[i].choose){
        deleteIdList.push(that.data.tempList[i].id)
        show_button = true;
      }
    }

    that.setData({
      itemClick: show_button,
    })
    var x = 0;
    var y = 0;
    if (show_button){
      x = 1;
      y = 1;
    }else{
      x = 0;
      y = 0;
    }
    scaleButton(this,x,y);
  }
})

var deleteIdList = [];
function getLongList(that){
  wx.showLoading({
    title: '加载中',
  })
  var mUserList = [];
  wx.request({
    url: Constant.BASEURL + Constant.GETLONG_HISTORY + '&openid=' + app.globalData.openid,
    method: 'GET',
    success: function(res){
      console.log(res.data)
      for(var i=0; i<res.data.data.length; i++){
        var personphoto = Constant.BASEURL + "/" + res.data.data[i].personphoto;
        // console.log("ccccccccccccccccc",res.data);
        var name = "姓名：" + res.data.data[i].personname;
        var relation = "关系：" + res.data.data[i].personrelation;
        var time = "录入时间 : " + res.data.data[i].createDate;
        var check = res.data.data[i].childcheck;
        var choose = false; //默认未被选中

        console.log("授权分享： ", check);
        mUserList.push({ "id": res.data.data[i].id, "personrelation": relation, "personname": name, "createTime":time,"personphoto": personphoto, "choose": choose,"childcheck":check})
      }
      that.setData({
        userList: mUserList,
        tempList:mUserList
      })

      if (res.data.data.length < 1) {
        wx.showToast({
          title: '还没有添加亲属',
          icon: 'none'
        })
      }
      wx.hideLoading();
      wx.stopPullDownRefresh();
    },
    fail: function(res){
      wx.hideLoading(); 
      wx.stopPullDownRefresh();
      wx.showToast({
        title: '系统错误',
        icon: 'none'
      })
    }
  })
}

function scaleButton(that,x,y){
  //创建动画
  var animation = wx.createAnimation({
    duration: 500,
    timingFunction: "ease",
    delay: 0,
    transformOrigin: "50% 50%",
  })
  animation.scale(x, y).step();     //边旋转边放大
  that.setData({
    animationData: animation.export(),
  })

  
}